package com.wisemoresystem.seal.action.useapply.send;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.wisemoresystem.seal.base.action.ProjBaseAction;
import com.wisemoresystem.seal.base.tip.Tip;
import com.wisemoresystem.seal.model.t_seal_use_apply;
import com.wisemoresystem.seal.model.t_user;
import com.wisemoresystem.seal.service.MacService;
import com.wisemoresystem.seal.service.UseApplyService;
import com.wisemoresystem.seal.service.WkflowService;
import com.wisemoresystem.seal.web.WebTools;

/**
 * 用印申请
 * 
 * @author Administrator
 *
 */
@Controller("ixSealUseApplySendAction")
@Scope("prototype")
@RequestMapping("/ix/useapply/apply/send")
public class SealUseApplySendAction extends ProjBaseAction {

	@Autowired
	private WkflowService wkService;
	@Autowired
	private UseApplyService uAService;
	@Autowired
	private MacService mService;

	/**
	 * 使用申请列表
	 * 
	 * @param srh_reason
	 *            用印事由
	 * @param srh_user
	 *            申请人
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list() {

		List<Map<String, Object>> list = uAService.selectToSendUseApplyList(
				null, null, page);

		ModelAndView mv = new ModelAndView("/jsp/useapply/send/list");
		mv.addObject("list", list);
		return mv;
	}

	@RequestMapping("/detail")
	public ModelAndView detail(String apply_id) {

		t_seal_use_apply apply = oc.findObjById(apply_id,
				t_seal_use_apply.class);
		t_user user = oc.findObjById(apply.getApply_user(), t_user.class);

		List<Map<String, Object>> macList = mService.selectMac(WebTools
				.getCurrentBankId());
		List<Map<String, Object>> sealList = uAService.selectSeal(WebTools
				.getCurrentBankId());
		Map<String, Object> files = uAService.getMapFiles(apply);

		List<Map<String, Object>> wkflowList = wkService.selectWkflowList();

		ModelAndView mv = new ModelAndView("/jsp/useapply/send/detail");
		mv.addObject("macList", macList);
		mv.addObject("sealList", sealList);
		mv.addObject("apply", apply);
		mv.addObject("user", user);
		mv.addObject("files", files);
		mv.addObject("wkflowList", wkflowList);
		return mv;
	}

	@RequestMapping("/send")
	public ModelAndView send(String apply_id, String office_opinion,
			String wkflow_id) {

		t_seal_use_apply apply = oc.findObjById(apply_id,
				t_seal_use_apply.class);
		apply.setOffice_opinion(office_opinion);
		oc.updateObj(apply);

		// 根据工作流，生成审批报表
		wkService.makeWkflow(apply_id, wkflow_id);

		List<Map<String, Object>> wkflowList = wkService.selectWkflowList();

		this.setTipMsg("发送成功！", Tip.Type.success);
		ModelAndView mv = new ModelAndView("redirect:list.htm");
		mv.addObject("wkflowList", wkflowList);
		return mv;
	}

}
